// packageJ/eyeDevice/eyesightRecord/index.js
const app = getApp();
const echarts = require("../../../ec-canvas/echarts"); //路径一定要正确否则会调用错误
// const { info } = require("../../../utils/logs");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ecIncome: {
      lazyLoad: true, // 延迟加载
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '视力数据档案',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toHistory(){
    wx.navigateTo({
      url: '/packageJ/eyeDevice/eyesightRecordhistory/index',
    })
  },
  toEntering() {
    wx.navigateTo({
      url: '/packageJ/eyeDevice/entering/entering',
    })
  },
  initForeach() {
    for (const key in this.data.infoData) {
      this.initEcharts(key)
    }
  },
  initEcharts(i) {
    let infoData = this.data.infoData;
    let echartsComponnet = this.selectComponent(`#echats-income${i}`);
    let maxMoney = Math.ceil(Math.max(...infoData[i].left_data)) || 50;
    let maxCount = Math.max(...infoData[i].right_data) || 50;
    let intervalMoney = maxMoney / 5 || 6;
    let intervalCount = maxCount / 5 || 6;
    let option = {
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "cross",
          crossStyle: {
            color: "#999",
          },
        },
      },
      toolbox: {
        feature: {
          magicType: {
            show: true,
            type: ["line", "bar"]
          },
          saveAsImage: {
            show: true
          },
        },
      },
      legend: {
        data: [{
          name: '左眼',
        }, {
          name: '右眼',
        }],
        x: 'right',
      },
      xAxis: [{
        type: "category",
        data:infoData[i].x_axis,
        axisPointer: {
          type: "shadow",
        },
      }, ],
      yAxis: [{
        type: "value",
        name: "",
        min: 0,
        max: maxMoney,
        interval: intervalMoney,
      }],
      series: [{
        name: "左眼",
        type: "line",
        color: "#015EF5",
        data: infoData[i].left_data,
      }, {
        name: "右眼",
        type: "line",
        color: "#1AC970",
        data: infoData[i].right_data,
      }, ],
    };

    echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height,
      });
      Chart.setOption(option);
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return Chart;
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.eye-device.frontend.modules.archive.controllers.chart.index");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            infoData: res.data
          })
          this.initForeach();
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  }
})